﻿using MVCforPhotoWall.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCforPhotoWall.Controllers
{
    public class BookListController : Controller
    {
        private IniFile ini;

        // GET: Login
        //public ActionResult Index()
        //{
        //    return View();
        //}
        // GET: Login
        public ActionResult Index()
        {
            ini = new IniFile(Server.MapPath("BookList\\books") + @"\MyConfig.INI");

            string rootpath = Server.MapPath("BookList\\books");
            List<FileModel> filse = Directory.GetFiles(rootpath, "*.txt")
                .Select(file => new FileModel
                {
                    FileName = Path.GetFileName(file),
                    FilePath = file,
                }
                ).ToList();

            ActionResult re = View(filse);
            return re;
        }
        public ActionResult Read(string fileName, int? lineNumber = null)
        {
            string rootPath = Server.MapPath("books");
            string filePath = Path.Combine(rootPath, fileName);
            ini = new IniFile(Server.MapPath("") + @"\MyConfig.INI");

            if (System.IO.File.Exists(filePath))
            {
                string[] lines = System.IO.File.ReadAllLines(filePath);
                int totalLines = lines.Length;

                int currentLineNumber = 0;
                if (lineNumber == null)
                {
                    currentLineNumber = ini.ReadInt(fileName, "页码", 0);
                }
                else
                {
                    currentLineNumber = lineNumber.Value;
                    ini.WriteInt(fileName, "页码", currentLineNumber);
                }
                if (currentLineNumber < 1)
                {
                    currentLineNumber = 1;
                }
                else if (currentLineNumber > totalLines)
                {
                    currentLineNumber = totalLines;
                }

                LineModel lineModel = new LineModel
                {
                    FileName = fileName,
                    Line = lines[currentLineNumber - 1],
                    LineNumber = currentLineNumber,
                    TotalLines = totalLines,
                    Note = ini.ReadString("PZ", lines[currentLineNumber - 1], ""),
                };

                return View(lineModel);
            }
            else
            {
                return HttpNotFound();
            }
        }

        [HttpPost]
        public ActionResult SaveNote(string fileName, int lineNumber,string lineWord, string note)
        {
            saveNote(lineWord, note);
            return RedirectToAction("Read", new { fileName = fileName, lineNumber = lineNumber });
        }

        private void saveNote(string lineWord, string note)
        {
            ini = new IniFile(Server.MapPath("") + @"\MyConfig.INI");

            ini.WriteString("PZ", lineWord, note);
        }

    }
}